/*******************************************************************************

Name: Table 4 - Raw differences in means for each disclosure threshold

Authors: Jonathan Baker, Lori Bennear, Sheila Olmstead

Input files:
	Primary panel: natlCWS_yr_Panel_mod-small_v2-Match.dta
		
*******************************************************************************/


* ========================== START START START ==========================
clear
clear matrix
set more off

* ______________________________________________________________________________
* set paths and create log file
global root "C:/Users/jbaker/Documents/Research/SDWIS/Analysis/STATA"
global input "$root/Input"
global intermediate "$root/Intermediate"
global log "$root/LogFiles"
global output "$root/Output"
global outpanel "$root/OutputPanel"
global figures "$root/Figures"

log using "$log/Table4_rawDiff-in-means.log", replace name(rawmeans)
set more off

* ============================================================================== 
* table of raw difference in means

mat mndif = J(3,7,.) // returns 3 x 7 matrix
local rcnt = 0
local threshold "501 10k 100k"

foreach req of local threshold {
	local rcnt = `rcnt' + 1
	use "$outpanel/natlCWS_yr_Panel_mod-small_v2-Match.dta", clear
		keep if id2001==1 & mflag`req' == 1
		expand freq_`req'

	gen T0pre`req' = HLTH_999 if year < 1998 & T_`req'==0
	gen T0post`req' = HLTH_999 if year >= 1998 & T_`req'==0
	gen T1pre`req' = HLTH_999 if year < 1998 & T_`req'==1
	gen T1post`req' = HLTH_999 if year >= 1998 & T_`req'==1
	
	tabstat T1pre`req' T1post`req' T0pre`req' T0post`req', stat(mean n) c(s)

	quietly summarize T1pre`req'
	local meanT1pre = r(mean)
	quietly summarize T1post`req'
	local meanT1post = r(mean)
	quietly summarize T0pre`req'
	local meanT0pre = r(mean)
	quietly summarize T0post`req'
	local meanT0post = r(mean)
		
	mat mndif[`rcnt',2] = `meanT1pre'
	mat mndif[`rcnt',1] = `meanT1post'
	mat mndif[`rcnt',3] = `meanT1post' - `meanT1pre'
	mat mndif[`rcnt',5] = `meanT0pre'
	mat mndif[`rcnt',4] = `meanT0post'
	mat mndif[`rcnt',6] = `meanT0post' - `meanT0pre'
	mat mndif[`rcnt',7] = (`meanT1post' - `meanT1pre') - (`meanT0post' - `meanT0pre')
	}

	mat rownames mndif = "Publishing" "Mailing" "Online"
	putexcel set "$output/Table4_rawDiff-in-means.xlsx", sheet(difinmean, replace) modify
	putexcel A1 = matrix(mndif), rownames nformat(number_d2)

	* ========================== DONE DONE DONE ==========================
log close rawmeans
